package com.redistest.demo.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import lombok.Data;
import redis.clients.jedis.JedisPoolConfig;

//主redis对应的配置类
@Data
@Configuration
public class SlaveRedisConfig extends RedisConfig {

	@Value("${spring.redis.slave.port}")
	private int port;

	@Value("${spring.redis.slave.host}")
	private String host;

	@Value("${spring.redis.slave.timeout}")
	private int timeout;

	// redis从连接工厂
	@Bean("slaveRedisFactory")
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
		return super.jedisConnectionFactory(jedisPoolConfig, port, host, timeout);
	}

	// 构造redis主对应的Template
	@Bean("slaveRedisTemplate")
	// @Primary
	public RedisTemplate<String, Object> redisTemplate(
			@Qualifier("slaveRedisFactory") RedisConnectionFactory jedisConnectionFactory) {
		return super.redisTemplate(jedisConnectionFactory);
	}

}